#include "StdAfx.h"
#include <tchar.h>
#include <Windows.h>
#include "Ini.h"


VOID CIni::InitstrFileName()
{
	this->m_strFileName = new TCHAR[MAX_PATH];
	GetModuleFileName(NULL,this->m_strFileName,MAX_PATH);
	INT i = _tcslen(this->m_strFileName);
	do 
	{
		if (this->m_strFileName[i] == '\\')
		{
			this->m_strFileName[i+1] = '\0';
			return;
		}
	} while (--i);
}

CIni::CIni()
{
	InitstrFileName();
	_tcscat(this->m_strFileName, TEXT("Config.ini"));
}

CIni::CIni(TCHAR *strFileName)
{
	InitstrFileName();
	if (strFileName[1] == ':')
		this->m_strFileName = strFileName;
	else
		_tcscat(this->m_strFileName, strFileName);
}

CIni::~CIni()
{
}

void CIni::GetAllSections(CStringArray *arr)
{
	static TCHAR strTemp[MAX_PATH];
	::GetPrivateProfileSectionNames(strTemp, sizeof(strTemp), this->m_strFileName);
	for(char* Name = strTemp; *Name != '\0'; Name += (strlen(Name) + 1))
		arr->Add(Name);
}

TCHAR* CIni::GetValue(TCHAR * strSection,TCHAR * strKeyName, TCHAR * def)
{
	static TCHAR strTemp[MAX_PATH];
	::GetPrivateProfileString(strSection, strKeyName, def, strTemp, sizeof(strTemp), this->m_strFileName);
	return strTemp;
}

LONG CIni::GetValue(TCHAR * strSection,TCHAR * strKeyName, LONG def)
{
	return ::GetPrivateProfileInt(strSection, strKeyName, def, this->m_strFileName);
}

double CIni::GetValue(TCHAR * strSection,TCHAR * strKeyName, double def)
{
	static TCHAR strTemp[MAX_PATH];
	static TCHAR strDef[MAX_PATH];
	_stprintf(strDef,TEXT("%f"),def);
	::GetPrivateProfileString(strSection, strKeyName, strDef, strTemp, sizeof(strTemp), this->m_strFileName);
	return _ttol(strTemp);
}

DWORD CIni::GetValue(TCHAR * strSection,TCHAR * strKeyName, DWORD def)
{
	return ::GetPrivateProfileInt(strSection, strKeyName, def, this->m_strFileName);
}

BOOL CIni::SetValue(TCHAR * strSection,TCHAR * strKeyName, TCHAR * set)
{
	return ::WritePrivateProfileString(strSection, strKeyName, set, this->m_strFileName);
}

BOOL CIni::SetValue(TCHAR * strSection,TCHAR * strKeyName, LONG set)
{
	TCHAR strTemp[MAX_PATH];
	_stprintf(strTemp,TEXT("%d"),set);
	return ::WritePrivateProfileString(strSection, strKeyName, strTemp, this->m_strFileName);
}

BOOL CIni::SetValue(TCHAR * strSection,TCHAR * strKeyName, double set)
{
	TCHAR strTemp[MAX_PATH];
	_stprintf(strTemp,TEXT("%f"),set);
	return ::WritePrivateProfileString(strSection, strKeyName, strTemp, this->m_strFileName);
}

BOOL CIni::SetValue(TCHAR * strSection,TCHAR * strKeyName, DWORD set)
{
	TCHAR strTemp[MAX_PATH];
	_stprintf(strTemp,TEXT("%u"),set);
	return ::WritePrivateProfileString(strSection, strKeyName, strTemp, this->m_strFileName);
}

BOOL CIni::DelKeyName(TCHAR * strSection,TCHAR * strKeyName)
{
	return ::WritePrivateProfileString(strSection,strKeyName,NULL,this->m_strFileName);
}

BOOL CIni::DelSection(TCHAR * strSection)
{
	return ::WritePrivateProfileString(strSection,NULL,NULL,this->m_strFileName);
}